﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace modDatos
{
    public class CRepositor
    {
        public byte[] dataByte;

        public Object Desempaquetar(byte[] paquete)
        {
            try
            {
                MemoryStream memStream = new MemoryStream();
                memStream.Position = 0;
                BinaryFormatter binForm = new BinaryFormatter();
                memStream.Write(paquete, 0, paquete.Length);
                memStream.Seek(0, SeekOrigin.Begin);
                object generic = binForm.Deserialize(memStream);
                return generic;
            }
            catch (Exception e)
            {
                string lala = e.ToString();
                Console.WriteLine(lala);
            }

            return null;
        }

        public byte[] Empaquetar(Object obj)
        {

            try
            {
                if (obj == null)
                    return null;
                BinaryFormatter bf = new BinaryFormatter();
                MemoryStream ms = new MemoryStream();
                bf.Serialize(ms, obj);
                return ms.ToArray();
            }
            catch (Exception e)
            {
                string lala = e.ToString();
                Console.WriteLine(lala);
                return null;
            }

        }
    }
}
